package com.steven.download.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.util.Log;

import com.steven.download.entity.AppEntity;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.FileReader;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Data：11/30/2017-10:40 AM
 *
 * @author: yanzhiwen
 */
public class Utils {
    private static SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");

    public static String md5Url(String url) {
        if (TextUtils.isEmpty(url)) {
            return url;
        }
        return md5(url);

    }

    private static String md5(String s) {
        try {
            MessageDigest digest = MessageDigest
                    .getInstance("MD5");
            digest.update(s.getBytes());
            byte messageDigest[] = digest.digest();

            return toHexString(messageDigest);
        } catch (NoSuchAlgorithmException e) {
            //LogDebugger.exception(e.getMessage());
        }
        return s;
    }

    private static final char HEX_DIGITS[] = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public static String toHexString(byte[] b) {
        StringBuilder sb = new StringBuilder(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);
            sb.append(HEX_DIGITS[b[i] & 0x0f]);
        }
        return sb.toString();
    }

    public static void close(Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static float keepTwoBit(float value) {
        DecimalFormat df = new DecimalFormat("0.00");
        return Float.parseFloat(df.format(value));
    }

    private static final String[] url = {"https://imtt.dd.qq.com/16891/apk/1962EE9CFA69AC65231F488F9B978B69.apk?fsname=com.tencent.mobileqq_8.8.0_1792.apk",
            "https://imtt.dd.qq.com/16891/apk/5C0FF221A948463BCF9F3255E0112034.apk?fsname=com.tencent.mm_8.0.6_1900.apk&csr=1bbd",
            "https://imtt.dd.qq.com/16891/apk/8B91D1C0FFFAE9EB75427B2A02FF82D2.apk?fsname=com.eg.android.AlipayGphone_10.2.23.7100_400.apk&csr=1bbd"};


    private static final String[] appIcons = {"http://e.hiphotos.bdimg.com/wisegame/wh%3D72%2C72/sign=a96dced3a151f3dec3e7b163a6c2c72d/30adcbef76094b3698d41df4adcc7cd98d109d60.jpg",
            "http://d.hiphotos.bdimg.com/wisegame/wh%3D72%2C72/sign=6c5fcff6c5fcc3ceb495c134a069e1ba/810a19d8bc3eb13518b5f46fab1ea8d3fc1f44f7.jpg",
            "http://d.hiphotos.bdimg.com/wisegame/wh%3D72%2C72/sign=279aea97adcc7cd9fa783cde0b2d160d/d439b6003af33a876265f160c85c10385343b544.jpg"};


    private static final String[] versions = {"8.8.0", "8.0.6", "10.2.23.7100"};
    private static final String[] sizes = {"113.79MB", "190.69MB", "95.54MB"};
    private static final String[] downLoadCounts = {"27.7亿", "34.3亿", "7.1亿"};

    private static final String[] names = {"QQ", "微信", "支付宝"};
    private static Context mContext;


    public static List<AppEntity> generateApp() {
        List<AppEntity> apps = new ArrayList<>();
        for (int i = 0; i < url.length; i++) {
            AppEntity appEntity = new AppEntity(url[i], appIcons[i], names[i], versions[i], sizes[i], downLoadCounts[i]);
            apps.add(appEntity);
        }
        return apps;
    }

    public static String getCreateTimeStr() {
        long millis = System.currentTimeMillis();
        return sf.format(millis);
    }
    public static void init(Application context) {
        mContext = context;
    }
    public static Context getContext() {
        if (mContext != null) return mContext;
        throw new NullPointerException("u should init first");
    }


    /**
     *   * 获取android总运行内存大小
     *   * @param context
     *   *
     */
    public static String getTotalMemory(Context context) {
        String str1 = "/proc/meminfo";// 系统内存信息文件
        String str2;
        String[] arrayOfString;
        long initial_memory = 0;
        try {
            FileReader localFileReader = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
            str2 = localBufferedReader.readLine();// 读取meminfo第一行，系统总内存大小
            arrayOfString = str2.split("\\s+");
            for (String num : arrayOfString) {
                Log.e("checkEmulator memory："+str2, num + "\t");
            }
            // 获得系统总内存，单位是KB
            int i = Integer.valueOf(arrayOfString[1]).intValue();
            //int值乘以1024转换为long类型
            initial_memory = new Long((long) i * 1024);
            localBufferedReader.close();
        } catch (IOException e) {
        }
        return Formatter.formatFileSize(context, initial_memory);// Byte转换为KB或者MB，内存大小规格化
    }


    /**
     * 判断某个界面是否在前台
     *
     * @param activity 要判断的Activity
     * @return 是否在前台显示
     */
    public static boolean isForeground(Activity activity) {
        return isForeground(activity, activity.getClass().getName());
    }

    /**
     * 判断某个界面是否在前台
     *
     * @param context   Context
     * @param className 界面的类名
     * @return 是否在前台显示
     */
    public static boolean isForeground(Context context, String className) {
        if (context == null || TextUtils.isEmpty(className))
            return false;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
        if (list != null && list.size() > 0) {
            ComponentName cpn = list.get(0).topActivity;
            if (className.equals(cpn.getClassName()))
                return true;
        }
        return false;
    }
}
